package com.example.city_home_app.Fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Bean.Down_News;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_News;
import com.example.city_home_app.RetrofitListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class Fragment_news extends BaseFragment {

    @BindView(R.id.rec_list_news)
    RecyclerView recListNews;
    Unbinder unbinder;

    private ApiModel model = new ApiModel();
    private SwipeRefreshLayout refreshLayout;
    private Adapter_News adapter_news;
    private RetrofitListener<List<Down_News>> listRetrofitListener = new RetrofitListener<List<Down_News>>() {
        @Override
        public void onResponse(List<Down_News> listener) {
            if(adapter_news.getItemCount() != 0) {
                adapter_news.clearList();
            }
            adapter_news.setList(listener);
            adapter_news.notifyDataSetChanged();
            refreshLayout.setRefreshing(false);
        }

        @Override
        public void onFail(String msg) {
            refreshLayout.setRefreshing(false);
        }
    };

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        recListNews = (RecyclerView) view.findViewById(R.id.rec_list_news);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recListNews.setLayoutManager(layoutManager);
        adapter_news = new Adapter_News(getActivity());
        recListNews.setAdapter(adapter_news);
        recListNews.setItemAnimator(new DefaultItemAnimator());

        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_list_news);
        refreshLayout.setColorSchemeColors(0xff49afcd);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        model.getnewlist(BaseActivity.getUserinfo().getCityid(),listRetrofitListener);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_news;
    }

    public void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        model.getnewlist(BaseActivity.getUserinfo().getCityid(),listRetrofitListener);

                    }
                });
            }
        }).start();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
